package text;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
class MinStack {

    public Stack<Integer> stack1;
    public Stack<Integer> stack2;
    public MinStack() {
         stack1 =new Stack<>();
         stack2 =new Stack<>();
    }

    public void push(int val) {
        if(stack1.empty()&&stack2.empty()){
            stack1.push(val);
            stack2.push(val);
        }else {
            if(stack2.peek()<val){
                stack1.push(val);
            }else{
                stack1.push(val);
                stack2.push(val);
            }
        }

    }

    public void pop() {
        if(stack1.empty()){
            return;
        }
        if(stack1.peek().equals(stack2.peek())){
            stack1.pop();
            stack2.pop();
        }else{
            stack1.pop();
        }
    }

    public int top() {
        if (stack1.empty()){
            return -1;
        }
        return stack1.peek();
    }

    public int getMin() {
        if (stack1.empty()){
            return -1;
        }
        return stack2.peek();
    }
}

















public class Text {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack =new Stack<>();
        int sum =0;
        for(int i=0;i<tokens.length;i++){
            if(tokens[i].equals("+")||tokens[i].equals("-")||tokens[i].equals("*")||tokens[i].equals("/")){
                int val2=stack.pop();
                int val1=stack.pop();
                switch(tokens[i]){
                    case "+" :
                        sum=val1+val2;
                        break;
                    case "-" :
                        sum=val1-val2;
                        break;
                    case "*" :
                        sum=val1*val2;
                        break;
                    case "/" :
                        sum=val1/val2;
                        break;
                }
                stack.push(sum);
            }else{
                stack.push(Integer.valueOf(tokens[i]));
            }
        }
        return stack.pop();
    }
    public boolean isValid(String s) {
        Stack<Character> stack1 =new Stack<>();
        for(int i=0;i<s.length();i++){
            char ch =s.charAt(i);
            if(ch=='('||ch=='{'||ch=='['){
                stack1.push(ch);
            }else{
                //字符串没遍历完的情况
                if(stack1.empty()){
                    return false;
                }
                //比较相不相等
                if(stack1.peek()=='(' && ch==')'||stack1.peek()=='{'&&ch=='}'||stack1.peek()=='['&&ch==']'){
                    stack1.pop();   //相等就弹出一个字符
                }else{
                    return false;
                }
            }
        }
        //栈中有剩余的情况
        if(stack1.empty()){
            return true;
        }else{
            return false;
        }
    }
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack =new Stack<>();
        int j=0;
        for(int i=0;i<pushV.length;i++){
            stack.push(pushV[i]);
            while(stack.peek()==popV[j]){
                stack.pop();
                j++;
                if (stack.empty()){
                    break;
                }
            }
        }
        return stack.empty();
    }

    public static void main(String[] args) {
        Stack<Integer> stack =new Stack<>();
        System.out.println(stack.empty());
    }




    public static void main3(String[] args) {
        int[] array1 =new int[]{2,1,0};
        int[] array2 =new int[]{1,2,0};
        boolean a=IsPopOrder(array1, array2);
        System.out.println(a);
    }
    public static void main2(String[] args) {
        LinkedList<Integer> stack =new LinkedList<>();
        Stack<Character> Stack1 =new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }
    public static void main1(String[] args) {
        Stack<Integer> stack =new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(5);
        System.out.println(stack);
    }
}
